<?php
session_start();
include_once 'admin/connect.php';

// 验证登录
if(!isset($_SESSION['loginadmin'])) {
    echo "<script>alert('请先登录后查看！');window.location.href='admin/login.php';</script>";
    exit;
}

// 获取文章ID
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if($id == 0) {
    echo "请提供文章ID";
    exit;
}

// 查询文章内容
$sql = "SELECT * FROM article WHERE id = ?";
$stmt = $connect->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$article = $result->fetch_assoc();

if(!$article) {
    echo "文章不存在";
    exit;
}

// 提取文章中的所有图片
preg_match_all('/<img[^>]*src=(["\'])([^"\']+)\1[^>]*>/i', $article['articletext'], $matches);
$images = $matches[2];
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图片路径调试</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        h1 { color: #333; }
        .image-debug { margin-bottom: 30px; border-bottom: 1px solid #eee; padding-bottom: 20px; }
        .image-info { margin-bottom: 10px; }
        .image-preview { max-width: 300px; border: 1px solid #ddd; padding: 5px; }
        .success { color: green; }
        .error { color: red; }
    </style>
</head>
<body>
    <h1>图片路径调试 - 文章ID: <?php echo $id; ?></h1>
    <p>文章标题: <?php echo htmlspecialchars($article['articletitle']); ?></p>
    <p>找到 <?php echo count($images); ?> 张图片</p>
    
    <?php foreach($images as $index => $src): ?>
    <div class="image-debug">
        <h3>图片 #<?php echo $index + 1; ?></h3>
        <div class="image-info">原始路径: <code><?php echo htmlspecialchars($src); ?></code></div>
        
        <?php
        // 检查路径是否以/开头
        if(substr($src, 0, 1) !== '/') {
            echo '<div class="image-info error">警告: 路径不是以/开头</div>';
            $src = '/' . $src;
            echo '<div class="image-info">修正路径: <code>' . htmlspecialchars($src) . '</code></div>';
        }
        
        // 检查文件是否存在
        $file_path = $_SERVER['DOCUMENT_ROOT'] . $src;
        if(file_exists($file_path)) {
            echo '<div class="image-info success">文件存在: ' . $file_path . '</div>';
        } else {
            echo '<div class="image-info error">文件不存在: ' . $file_path . '</div>';
            
            // 尝试其他可能的路径
            $alt_paths = [
                str_replace('/uploads/images/', '/uploads/', $src),
                '/uploads/' . basename($src),
                '/uploads/images/' . basename($src)
            ];
            
            echo '<div class="image-info">尝试其他路径:</div>';
            echo '<ul>';
            foreach($alt_paths as $alt_path) {
                $alt_file_path = $_SERVER['DOCUMENT_ROOT'] . $alt_path;
                if(file_exists($alt_file_path)) {
                    echo '<li class="success">文件存在: ' . $alt_file_path . ' (<code>' . $alt_path . '</code>)</li>';
                } else {
                    echo '<li class="error">文件不存在: ' . $alt_file_path . ' (<code>' . $alt_path . '</code>)</li>';
                }
            }
            echo '</ul>';
        }
        ?>
        
        <div class="image-preview">
            <img src="<?php echo htmlspecialchars($src); ?>" alt="预览" onerror="this.onerror=null;this.src='/Style/images/image-placeholder.png';this.alt='图片加载失败';">
        </div>
    </div>
    <?php endforeach; ?>
    
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // 检查所有图片加载状态
        var images = document.querySelectorAll('img');
        images.forEach(function(img) {
            img.addEventListener('load', function() {
                var infoDiv = this.parentNode.previousElementSibling;
                if(infoDiv) {
                    var loadSuccess = document.createElement('div');
                    loadSuccess.className = 'image-info success';
                    loadSuccess.textContent = '图片加载成功';
                    infoDiv.parentNode.insertBefore(loadSuccess, this.parentNode);
                }
            });
            
            img.addEventListener('error', function() {
                var infoDiv = this.parentNode.previousElementSibling;
                if(infoDiv) {
                    var loadError = document.createElement('div');
                    loadError.className = 'image-info error';
                    loadError.textContent = '图片加载失败';
                    infoDiv.parentNode.insertBefore(loadError, this.parentNode);
                }
            });
        });
    });
    </script>
</body>
</html> 